import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.pdfbox.cos.COSArray;
import org.apache.pdfbox.cos.COSObject;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.interactive.action.type.PDAction;
import org.apache.pdfbox.pdmodel.interactive.documentnavigation.destination.PDDestination;
import org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDOutlineItem;
import org.apache.pdfbox.util.PDFTextStripper;


public abstract class Seccion {

	private String name;
	private String texto;
	private String paginaInicial;
	private String paginaFinal;
		
	
	protected String getName() {
		return name;
	}
	protected void setName(String name) {
		this.name = name;
	}
	protected String getTexto() {
		return texto;
	}
	protected void setTexto(int inicialPage, PDDocument doc, int finalPage) {
		PDFTextStripper stripper;
		try {	 
			 stripper = new PDFTextStripper();	
			 stripper.setStartPage(Integer.parseInt(this.getPaginaInicial()));
			 stripper.setEndPage(Integer.parseInt(this.getPaginaFinal())-1);
			 String original = stripper.getText(doc);
			 this.texto = original;
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	protected String getPaginaInicial() {
		return paginaInicial;
	}
	
	protected void setPaginaInicial(PDOutlineItem item, PDDocument doc) {
		this.paginaInicial = String.valueOf(getPage(item, doc));
	}
	
	protected String getPaginaFinal() {
		return paginaFinal;
	}
	
	protected void setPaginaFinal(PDOutlineItem item, PDDocument doc, PDOutlineItem tope) {
		String page = null;
		if(item.getNextSibling() != null){
			page = String.valueOf(getPage(item.getNextSibling(), doc));					
		}else{
			if(tope != null){
				page = String.valueOf(getPage(tope, doc));
			}else{
				page = String.valueOf(doc.getNumberOfPages());
			}
		}
		this.paginaFinal = page;
	}
	
	public int getPage(PDOutlineItem current, PDDocument doc){
		 
		try{
	        if (current != null) {
	        	PDDestination  dest = current.getDestination();
	        	PDAction pdAction = current.getAction();
	            if (pdAction != null) {
	                // From BM-Thread
	                COSObject targetPageRef = (COSObject) ((COSArray) current.getAction().getCOSDictionary().getDictionaryObject("D")).get(0);
	                String objStr = String.valueOf(targetPageRef.getObjectNumber().intValue());
	                String genStr = String.valueOf(targetPageRef.getGenerationNumber().intValue());
	                return (Integer) doc.getPageMap().get(objStr + "," + genStr);
	            } else if (dest != null) {
	                // From PDP-Thread
	                PDPage pdp = current.findDestinationPage(doc);
	                List allpages = new ArrayList();
	                doc.getDocumentCatalog().getPages().getAllKids(allpages);
	                return allpages.indexOf(pdp) + 1;
	            }
	        }
		}catch(Exception e){
			// TODO Auto-generated catch block
			e.printStackTrace();	   
		}
		return 0;
	}
	
	protected abstract boolean isSeccionSimple();
	protected abstract String read();
	protected abstract Seccion parse(PDOutlineItem item, PDDocument doc, PDOutlineItem tope);
}
